﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4T.Data;
using System.Web.UI.HtmlControls;
using _4T.BLL;
using System.Web.Services;

namespace _4T
{
    public partial class Default : System.Web.UI.Page
    {
        private static string personalCode;

        private ProfileStageLinkBLL stageBLL;
        public ProfileStageLinkBLL StageBLL
        {
            get
            {
                if (stageBLL == null) stageBLL = new ProfileStageLinkBLL();
                return stageBLL;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurrentUser"] != null && Session["CurrentUser"].ToString().Trim().Equals(string.Empty) == false)
            {
                personalCode = Session["User"].ToString().Trim();
                if (!Page.IsPostBack)
                {
                    PQTPager1.CurrentPageIndex = 0;
                    BindProfileList();
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }

        [WebMethod]
        public static void DeleteProfile(string profileCode)
        {
            if (!string.IsNullOrEmpty(profileCode) && personalCode != null && personalCode.Trim() != string.Empty)
                new ProfileStageLinkBLL().DeleteProfile(personalCode, profileCode);
        }

        private void BindProfileList()
        {
            ProfileBLL profileBLL = new ProfileBLL();

            int bynRecycle = -1;

            List<ProfileInfoAndStatus> list = profileBLL.GetListProfileInfoAndStatus(PQTPager1.CurrentPageIndex + 1, PQTPager1.PageSize, "", bynRecycle);
            rptProfileList.DataSource = list;
            rptProfileList.DataBind();
            ProfileInfoBLL profileInfoBLL = new ProfileInfoBLL();
            int total = profileInfoBLL.GetTotalNumberProfile(bynRecycle);
            if (total > 0)
            {
                PQTPager1.RecordCount = total;
            }
        }

        protected void rptProfileList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ProfileStageLinkBLL stageBLL = new ProfileStageLinkBLL();
            ProfileInfoAndStatus item = (ProfileInfoAndStatus)e.Item.DataItem;
            //
            //
            //

            if (item.BinRecycle)
                ((HtmlControl)e.Item.FindControl("ProfileIsDeleted")).Style["display"] = "block";
            else
                ((HtmlControl)e.Item.FindControl("ProfileIsDeleted")).Style["display"] = "none";

            if (item.Stage == 6)
            {
                //((HtmlControl)e.Item.FindControl("UserAction")).Style["display"] = "none";
            }
            else
            {
                if (item.Status == 0 && item.BinRecycle == false)
                {
                    switch (item.Stage)
                    {
                        case 2:
                            ((HyperLink)e.Item.FindControl("LinkUserAction")).NavigateUrl = "CMS_ProfileFilterForm.aspx?p=" + item.ProfileCode;
                            ((Label)e.Item.FindControl("UserAction")).Text = "Sơ Tuyển";
                            break;
                        case 3:
                            ((HyperLink)e.Item.FindControl("LinkUserAction")).NavigateUrl = "ProfileValidate.aspx?pid=" + item.ProfileCode;
                            ((Label)e.Item.FindControl("UserAction")).Text = "Xác minh";
                            break;
                        case 4:
                            try
                            {
                                _4T.Data.IDataContext.CMSConsiderAndDecide cmsConsiderAndDecide = new DAL.ConsiderAndDecideDAL().GetConsiderAndDecideByProfile(item.ProfileCode);
                                ((HyperLink)e.Item.FindControl("LinkUserAction")).NavigateUrl = "ProfileApprove.aspx?pid=" + cmsConsiderAndDecide.ConsiderAndDecideID;
                                ((Label)e.Item.FindControl("UserAction")).Text = "Phê duyệt";
                            }
                            catch
                            {
                            }
                            break;
                        case 5:
                            try
                            {
                                _4T.Data.CMSApproveProfile cmsApproveProfile = new DAL.ApproveProfileDAL().GetProfileApproveByApproveProfileName(item.ProfileCode);
                                ((HyperLink)e.Item.FindControl("LinkUserAction")).NavigateUrl = "ProfilePayment.aspx?pid=" + cmsApproveProfile.ApproveProfileID;
                                ((Label)e.Item.FindControl("UserAction")).Text = "Thanh toán";
                            }
                            catch
                            {
                            }
                            break;
                    }
                }
            }
            //
            //
            //
            switch (item.Stage)
            {
                case 3:
                    {
                        BindFilter(item, e);
                        if (item.Status != 0)
                        {
                            BindVerify(item, e);
                        }
                        ShowCommentStage2(item, e);
                        ShowCommentStage3(item, e);
                    }
                    break;
                case 4:
                    {
                        BindFilter(item, e);
                        BindVerify(item, e);
                        if (item.Status != 0)
                        {
                            BindApprove(item, e);
                        }
                        ShowCommentStage2(item, e);
                        ShowCommentStage3(item, e);
                        ShowCommentStage4(item, e);
                    }
                    break;
                case 5:
                    {
                        BindFilter(item, e);
                        BindVerify(item, e);
                        BindApprove(item, e);
                        if (item.Status != 0)
                        {
                            BindPayment(item, e);
                        }

                        ShowCommentStage2(item, e);
                        ShowCommentStage3(item, e);
                        ShowCommentStage4(item, e);
                    }
                    break;
                case 6:
                    {
                        BindFilter(item, e);
                        BindVerify(item, e);
                        BindApprove(item, e);
                        BindPayment(item, e);

                        ShowCommentStage2(item, e);
                        ShowCommentStage3(item, e);
                        ShowCommentStage4(item, e);
                    }
                    break;
                default:
                    {
                        BindFilter(item, e);
                        ShowCommentStage2(item, e);
                    }
                    break;
            }
        }

        protected void PQTPager1_PageIndexChanged(object sender, PQT.PageIndexChangedEventArgs e)
        {
            PQTPager1.CurrentPageIndex = e.NewPageIndex;
            BindProfileList();
        }

        private void ShowCommentStage2(ProfileInfoAndStatus item, RepeaterItemEventArgs e)
        {
            if (item.Stage >= 2)
                ((HtmlControl)e.Item.FindControl("FrameCommentFilter")).Style["display"] = "block";

            if (item.Stage == 2)
                ((HtmlControl)e.Item.FindControl("CommentFormFilter")).Style["display"] = "block";

            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Repeater rptCommentFilter = (Repeater)e.Item.FindControl("rptCommentFilter");
                if (rptCommentFilter != null)
                {
                    rptCommentFilter.DataSource = new DAL.CommentDAL().GetListComment(item.ProfileCode, 2);
                    rptCommentFilter.DataBind();
                }
            }
        }

        private void ShowCommentStage3(ProfileInfoAndStatus item, RepeaterItemEventArgs e)
        {
            if (item.Stage >= 3)
                ((HtmlControl)e.Item.FindControl("FrameCommentVerifier")).Style["display"] = "block";

            if (item.Stage == 3)
                ((HtmlControl)e.Item.FindControl("CommentFormVerifier")).Style["display"] = "block";
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Repeater rptCommentVerifier = (Repeater)e.Item.FindControl("rptCommentVerifier");
                if (rptCommentVerifier != null)
                {
                    rptCommentVerifier.DataSource = new DAL.CommentDAL().GetListComment(item.ProfileCode, 3);
                    rptCommentVerifier.DataBind();
                }
            }
        }

        private void ShowCommentStage4(ProfileInfoAndStatus item, RepeaterItemEventArgs e)
        {
            if (item.Stage >= 4)
                ((HtmlControl)e.Item.FindControl("FrameCommentApprove")).Style["display"] = "block";

            if (item.Stage == 4)
                ((HtmlControl)e.Item.FindControl("CommentFormApproval")).Style["display"] = "block";
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Repeater rptCommentApprove = (Repeater)e.Item.FindControl("rptCommentApprove");
                if (rptCommentApprove != null)
                {
                    rptCommentApprove.DataSource = new DAL.CommentDAL().GetListComment(item.ProfileCode, 4);
                    rptCommentApprove.DataBind();
                }
            }
        }

        private void BindVerify(ProfileInfoAndStatus item, RepeaterItemEventArgs e)
        {
            ((HtmlControl)e.Item.FindControl("divVerify")).Style["display"] = "block";
            if (item.Status == 2 && item.Stage == 3)
            {
                ((HtmlControl)e.Item.FindControl("tdVerify")).Style["background-color"] = "Red";
                ((Label)e.Item.FindControl("lbVerifyResult")).Text = "Không trình duyệt";
            }
            else
            {
                ((Label)e.Item.FindControl("lbVerifyResult")).Text = "Trình duyệt";
            }
            ((Label)e.Item.FindControl("lbVerifier")).Text = item.Verifier;
            ProfileStageLink stage1 = StageBLL.GetProfileStageLinkByProfileCodeAndStage(item.ProfileCode, 3);
            if (stage1 != null)
            {
                ((Label)e.Item.FindControl("lbVerifyDate")).Text = stage1.EditTime;
                ((Label)e.Item.FindControl("lbVerifyContent")).Text = stage1.Description;
                if (!string.IsNullOrEmpty(stage1.VideoLink))
                {
                    string videoid = "videoVerify" + item.ProfileCode;
                    Page.ClientScript.RegisterStartupScript(GetType(), "playverifyvideo" + item.ProfileCode, "setupvideo(" + videoid + ", " + stage1.VideoLink + ");", true);
                }
            }
        }

        private void BindFilter(ProfileInfoAndStatus item, RepeaterItemEventArgs e)
        {
            if (((Session["CurrentUser"] as PersonalInfo).Role == (int)CMS.Core.Libs.UserRole.Paymenter && item.BinRecycle == false) || (Session["CurrentUser"] as PersonalInfo).Role == (int)CMS.Core.Libs.UserRole.Admin)
                ((HtmlControl)e.Item.FindControl("divAction")).Style["display"] = "block";
            ((HtmlControl)e.Item.FindControl("divFilter")).Style["display"] = "block";
            if (item.Status == 2 && item.Stage == 2) ((HtmlControl)e.Item.FindControl("tdFilter")).Style["background-color"] = "Red";
            ProfileStageLink stage1 = StageBLL.GetProfileStageLinkByProfileCodeAndStage(item.ProfileCode, 1);
            if (item.Filter != null && item.Filter != "")
                ((Label)e.Item.FindControl("lbFilterer")).Text = item.Filter;
            else
                ((Label)e.Item.FindControl("lbFilterer")).Text = "(Chưa sơ tuyển)";
            if (stage1 != null)
            {
                ((Label)e.Item.FindControl("lbProposeDate")).Text = stage1.EditTime;
                ProfileStageLink stage2 = StageBLL.GetProfileStageLinkByProfileCodeAndStage(item.ProfileCode, 2);
                if (stage2 != null)
                {
                    ((Label)e.Item.FindControl("lbFilterContent")).Text = stage2.Description;
                    ((Label)e.Item.FindControl("lbFilterDate")).Text = stage2.EditTime;
                }
                else
                {
                    ((Label)e.Item.FindControl("lbFilterDate")).Text = "(Chưa sơ tuyển)";
                }

                if (!string.IsNullOrEmpty(stage1.VideoLink))
                {
                    string videoid = "videoFilter" + item.ProfileCode;
                    Page.ClientScript.RegisterStartupScript(GetType(), "playfiltervideo" + item.ProfileCode, "setupvideo('" + videoid + "', '" + stage1.VideoLink + "');", true);
                }

            }
        }

        private void BindApprove(ProfileInfoAndStatus item, RepeaterItemEventArgs e)
        {
            ((HtmlControl)e.Item.FindControl("divApprove")).Style["display"] = "block";
            if (item.Status == 2 && item.Stage == 4) ((HtmlControl)e.Item.FindControl("tdApprove")).Style["background-color"] = "Red";
            ((Label)e.Item.FindControl("lbApprover")).Text = item.Approver;
            ((Label)e.Item.FindControl("lbMoney")).Text = item.Money;
            ProfileStageLink stageLink = StageBLL.GetProfileStageLinkByProfileCodeAndStage(item.ProfileCode, 4);
            if (stageLink != null)
            {
                ((Label)e.Item.FindControl("lbApproveDate")).Text = stageLink.EditTime;
                ((Label)e.Item.FindControl("lbApproveContent")).Text = stageLink.Description;
                if (!string.IsNullOrEmpty(stageLink.VideoLink))
                {
                    string videoid = "videoApprove" + item.ProfileCode;
                    Page.ClientScript.RegisterStartupScript(GetType(), "playapprovevideo" + item.ProfileCode, "setupvideo(" + videoid + ", " + stageLink.VideoLink + ");", true);
                }
            }
            //if (((Session["CurrentUser"] as PersonalInfo).Role == (int)CMS.Core.Libs.UserRole.Approver) || ((Session["CurrentUser"] as PersonalInfo).Role == (int)CMS.Core.Libs.UserRole.Paymenter) || ((Session["CurrentUser"] as PersonalInfo).Role == (int)CMS.Core.Libs.UserRole.Admin))
            //((HtmlControl)e.Item.FindControl("commentApprove")).Style["display"] = "block";
        }

        private void BindPayment(ProfileInfoAndStatus item, RepeaterItemEventArgs e)
        {
            ((HtmlControl)e.Item.FindControl("divPayment")).Style["display"] = "block";
            if (item.Status == 2 && item.Stage == 5) ((HtmlControl)e.Item.FindControl("tdPayment")).Style["background-color"] = "Red";
            ((Label)e.Item.FindControl("lbPaymentPerson")).Text = item.PaymentPerson;
            ((Label)e.Item.FindControl("lbPaymentAmount")).Text = item.PaymentAmount;
            ProfileStageLink stageLink = StageBLL.GetProfileStageLinkByProfileCodeAndStage(item.ProfileCode, 5);
            if (stageLink != null)
            {
                ((Label)e.Item.FindControl("lbPaymentDate")).Text = stageLink.EditTime;
                ((Label)e.Item.FindControl("lbPaymentContent")).Text = stageLink.Description;
                if (!string.IsNullOrEmpty(stageLink.VideoLink))
                {
                    string videoid = "videoPayment" + item.ProfileCode;
                    Page.ClientScript.RegisterStartupScript(GetType(), "playpaymentvideo" + item.ProfileCode, "setupvideo(" + videoid + ", " + stageLink.VideoLink + ");", true);
                }
            }
        }
    }
}